﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneChangeTrigger : MonoBehaviour
{
    public string sceneName;
    public Vector3 pos;
    public Vector3 offset = new Vector3(0,0.4f,0);
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag=="Player")
        {
            if (UIManager.Instance == null)
            {
                return;
            }
            UIManager.Instance.OpenSaveTip("C", transform, offset);
            

        }
    }
    public void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.tag == "Player")
        {
            if (UIManager.Instance == null)
            {
                return;
            }
            UIManager.Instance.CloseSaveTip();
        }
    }
    public void OnTriggerStay2D(Collider2D collision)
    {
        if (collision.tag == "Player")
        {
            if (Input.GetKeyDown(KeyCode.C))
            {
                switch (sceneName)
                {
                    case "Level1":
                    case "Level2":
                        GameManager.Instance.AsyncLoadScene(sceneName, () => {
                            collision.transform.position = pos;
                        });
                        break;
                    case "GameMain":
                        GameManager.Instance.AsyncLoadScene(sceneName, () => {
                            GameManager.Instance.ReStartGame();
                        });
                        break;
                }
                
            }
        }
    }

    public void OnDestroy()
    {
        if (UIManager.Instance == null)
        {
            return;
        }
        UIManager.Instance.CloseSaveTip();
    }
}
